﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Text;

namespace Age.Qna.Site.Mvc.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        public ActionResult Index()
        {

          Util.CategoryTree category = new Util.CategoryTree() 
          { 
            CategoryURL = "http://localhost:3239/link.do?c=", 
            UseCheckBox = true 
          };
          ViewBag.Categories = category.RenderCategoryUL();
          ViewBag.CategoryModPanel = category.RenderCategoryModPanel();


          return View();
        }

				[HttpPost]
				public JsonResult CategoryData(int id)
				{

					Qnawsvc.QnaWSvcClient client = new Qnawsvc.QnaWSvcClient("ws"); // TODO[P1]: Get "ws" from config
					IList<Qnawsvc.CategoryEntity> entity = client.GetCategories(id);
					client.Close();

					// Convert entity to json
					JavaScriptSerializer jss = new JavaScriptSerializer();
					StringBuilder jsonDatab = new StringBuilder();

					jss.Serialize(entity, jsonDatab);

					JsonResult result = new JsonResult()
					{
						JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet,
						ContentEncoding = Encoding.UTF8,
						ContentType = "application/json",
						Data = jsonDatab.ToString()
					};
					
					return result; //Json(jsonDatab.ToString(), JsonRequestBehavior.AllowGet);
				}

				[HttpPost]
				public JsonResult CategoryTreeData()
				{
					Qnawsvc.QnaWSvcClient client = new Qnawsvc.QnaWSvcClient("ws"); // TODO[P1]: Get "ws" from config
					IList<Qnawsvc.CategoryTreeEntity> entity = client.GetCategoryTree();
					client.Close();

					// Convert entity to json
					JavaScriptSerializer jss = new JavaScriptSerializer();
					StringBuilder jsonDatab = new StringBuilder();

					jss.Serialize(entity, jsonDatab);

					JsonResult result = new JsonResult()
					{
						JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet,
						ContentEncoding = Encoding.UTF8,
						ContentType = "application/json",
						Data = jsonDatab.ToString()
					};

					return result; 
				}

    }
}
